package com.util;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * Created by mayufeng on 2017/4/2.
 * 字符串过滤servlet
 */
@WebFilter(filterName = "charFilter",urlPatterns = "/*")
public class CharactorFilter implements Filter {

    String encoding="UTF-8";
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        if(encoding!=null){
            //设置request字符编码
            request.setCharacterEncoding(encoding);
            //设置response字符编码
            response.setContentType("text/html;charset="+encoding);
        }
        //传递给下一个过滤器
        chain.doFilter(request, response);
    }
    public void init(FilterConfig filterConfig) throws ServletException {
        //获取初始化参数
        encoding="UTF-8";
    }
    public void destroy() {
        // TODO Auto-generated method stub
        encoding=null;
    }
}
